#include <reg52.h>

//74LS139译码器输入端
sbit A24=P2^4;
sbit B24=P2^5;


//数码管显示数字（共阳）
unsigned char ledChar[]={
	0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,
	0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E
};
unsigned char targetLed=0;
int sec=0;
int curMs=0;
unsigned char ledsCode[8];
unsigned char flag1s=0;
void main(){
	EA=1;
	TMOD=0x10;
	TH1=0xFC;
	TL1=0x67;
	ET1=1;
	TR1=1;
	
	//高位
	ledsCode[3]=6;	

	//底三位
	ledsCode[0]=sec%10;
	ledsCode[1]=sec/10%10;
	ledsCode[2]=sec/100%10;
	while(1){
		if(flag1s==1){
			flag1s=0;
			//一秒到了
			if(sec<=999){
				sec++;
				ledsCode[0]=sec%10;
				ledsCode[1]=sec/10%10;
				ledsCode[2]=sec/100%10;
			}else{
				sec=0;
			}
		}		
	}
}

//中断时该干嘛
void InterruptTimer1() interrupt 3{
	TH1=0xFC;
	TL1=0x67;
	curMs++;
	if(curMs>=1000){
		curMs=0;
		flag1s=1;
	}
	P0 = 0;
	switch(targetLed){
		case 0:B24=0;A24=0;targetLed++;P0=~ledChar[ledsCode[0]];break;
		case 1:B24=0;A24=1;targetLed++;P0=~ledChar[ledsCode[1]];break;
		case 2:B24=1;A24=0;targetLed++;P0=~ledChar[ledsCode[2]];break;
		case 3:B24=1;A24=1;targetLed=0;P0=~ledChar[ledsCode[3]];break;
		default:break;
	}
}
